<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use service\LogService;
use think\Db;

/**
 * 后台参数配置控制器
 * Class Config
 * @package app\admin\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/02/15 18:05
 */
class Config extends BasicAdmin
{

    /**
     * 当前默认数据模型
     * @var string
     */
    public $table = 'SystemConf';

    /**
     * 当前页面标题
     * @var string
     */
    public $title = '网站参数配置';

    /**
     * 参数配置列表
     *
     * @return void
     * @author leolei <346991581@qq.com>
     */
    public function index()
    {
        $list = Db::name($this->table)->group('group_id')->column('group_id');
        $actions = $this->_filterAction($list);
        $this->assign('actions', $actions);
        $db = Db::name($this->table)->order('id desc');
        $db->where('is_deleted', '0');
        $db->where('state', '1');
        $get = $this->request->get();
        foreach (['name', 'group_id'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "{$get[$key]}");
            }
        }
        return parent::_list($db);
    }

    /**
     * 首页查询参数处理
     *
     * @param array $action
     * @return void
     * @author leolei <346991581@qq.com>
     */
    private function _filterAction($action)
    {
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
        $groupArr = dict_arr('conf_group');
        $return = [];
        foreach ((array)$action as $key => $value) {
            $return[$key]['name'] = $groupArr[$value]?$groupArr[$value]:$value;
            $return[$key]['value'] = $value;
        }
        return $return;
    }

    /**
     * 列表数据处理
     * @param $data
     */
    protected function _index_data_filter(&$data)
    {
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
        $groupArr = dict_arr('conf_group');
        $typeArr = dict_arr('input_type');
        foreach ($data as &$vo) {
            $vo['group_id_txt'] = $groupArr[$vo['group_id']]?$groupArr[$vo['group_id']]:$vo['group_id'];
            $vo['input_type_txt'] = $typeArr[$vo['input_type']];
        }
    }

    /**
     * 配置添加
     *
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 配置编辑
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 表单数据前缀方法
     * @param array $vo
     */
    protected function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            $this->assign('groups', dict_list('conf_group'));
            $this->assign('types', dict_list('input_type'));
        }
    }

    /**
     * 配置删除
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("配置删除成功！", '');
        }
        $this->error("配置删除失败，请稍候再试！");
    }

    /**
     * 参数展示
     *
     * @param string $group_id
     * @return void
     * @author leolei <346991581@qq.com>
     */
    public function setting($group_id = '1')
    {
        if (!$this->request->isPost()) {
            //获取类型
            $title = Db::name('SystemDict')->where(['type'=>'conf_group','value'=>$group_id])->value('name');
            //获取列表
            $list = Db::name($this->table)->where(['group_id'=>$group_id,'is_deleted'=>'0','state'=>'1'])->order('sort asc')->select();
            $this->assign('title', $title);
            $this->assign('list', $list);
            return view();
        }
        foreach ($this->request->post() as $key => $vo) {
            sysconf($key, $vo);
        }
        LogService::write('系统管理', '修改系统配置参数成功');
        $this->success('数据修改成功！', '');
    }
}
